package _04StackAndQueue;

class ArrayStack {
    static final int STACK_SIZE = 1000;
    private int top;
    private int a[] = new int[STACK_SIZE];

    ArrayStack() {
        top = -1;
    }

    /**
     * 入栈
     * @param x
     * @return
     */
    boolean push(int x) {
        if (top >= (STACK_SIZE - 1)) {
            System.out.println("Stack overflow");
            return false;
        }
        else {
            a[++top] = x;
            return true;
        }
    }








    /**
     * 出栈
     * @return
     */
    int pop() {
        if (top < 0) return 0;
        else {
            int x = a[top--];
            return x;
        }
    }

    /**
     * 窥探
     * @return
     */
    int peek() {
        if (top < 0) return 0;
        else {
            int x = a[top];
            return x;
        }
    }

    /**
     * 是否为空
     * @return
     */
    boolean isEmpty() {
        return (top < 0);
    }
}

class Test {
    public static void main(String args[]) {
        ArrayStack s = new ArrayStack();
        s.push(1);
        s.push(2);
        s.push(3);

        System.out.println("peek: " + s.peek());
        System.out.println("pop: " + s.pop());
        System.out.println(s.isEmpty());
    }
}